package project.android;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;

public class TaskPage extends Activity{

	
	int id;
	String name=null;
    String description=null;
	String deadline=null;
	int finished;
	String assignedby = null;
	String groupname = null;
	String groupid = null;
	String guid = null;
	int deleted;
	
	
    public void onCreate(Bundle savedInstanceState) {
		
        super.onCreate(savedInstanceState);
		
        //get name of the current task
        Bundle extras = getIntent().getExtras(); 
        guid = extras.getString("taskGuid");
        
        //connect to database to get the information of the current task
        DatabaseConnection db = new DatabaseConnection(this);
        db.connectDatabase("TaskBoard","Tasks");
        Cursor c = db.select("GUID" , guid);
        
		if (c.getCount() != 0) {
				
			id = Integer.parseInt(c.getString(c.getColumnIndex("ID")));
			name = c.getString(c.getColumnIndex("NAME"));
			description = c.getString(c.getColumnIndex("DESCRIPTION"));
			deadline = c.getString(c.getColumnIndex("DEADLINE"));
			finished = c.getInt(c.getColumnIndex("FINISHED"));
			assignedby = c.getString(c.getColumnIndex("ASSIGNEDBY"));
			groupname =  c.getString(c.getColumnIndex("GROUPNAME"));
			groupid = c.getString(c.getColumnIndex("GROUPID"));
			deleted = c.getInt(c.getColumnIndex("DELETED"));
			
		}
		db.close();
		
		//set view items
		setContentView(R.layout.task_page);
		EditText ed = (EditText)findViewById(R.id.namein);  
        ed.setText(name);
        ed.setEnabled(false);
        ed = (EditText)findViewById(R.id.descriptionin);  
        ed.setText(description);
        ed.setEnabled(false);
        ed = (EditText)findViewById(R.id.groupnamein);
        ed.setText(groupname);
        ed.setEnabled(false);
        ed = (EditText)findViewById(R.id.assignedbyin);
        ed.setText(assignedby);
        ed.setEnabled(false);
        ed = (EditText)findViewById(R.id.deadlinein);
        ed.setText(deadline.toString());
        ed.setEnabled(false);
        ed = (EditText)findViewById(R.id.assignedtoin);
        ed.setText(Synchronized.username);
        ed.setEnabled(false);
        final CheckBox ch = (CheckBox)findViewById(R.id.finishedCheckBox);
        ch.setEnabled(false);
        if (finished == 1)
        	ch.setChecked(true);
        final CheckBox ch2 = (CheckBox)findViewById(R.id.overdateCheckBox);
        Date currentDate = new Date();
        if(Date.parse(deadline) <= currentDate.getTime() && finished == 0)
        	ch2.setChecked(true);
        ch2.setEnabled(false);
        
        
    }
    
    public void editButtonOnClick(View view){
    	
    	//change view for editting
    	EditText ed = (EditText)findViewById(R.id.namein);  
        ed.setEnabled(true);
    	ed = (EditText)findViewById(R.id.groupnamein);  
        ed.setEnabled(true);
        ed = (EditText)findViewById(R.id.descriptionin);  
        ed.setEnabled(true);
        ed = (EditText)findViewById(R.id.deadlinein);
        ed.setEnabled(true);
        ed = (EditText)findViewById(R.id.assignedbyin);
        ed.setEnabled(true);
        CheckBox ch = (CheckBox)findViewById(R.id.finishedCheckBox);
        ch.setEnabled(true);
    		
    }

	public void saveButtonOnClick(View view){
		
		//assign the inputs to variables
		name = ((EditText) findViewById(R.id.namein)).getText().toString();
		description = ((EditText) findViewById(R.id.descriptionin)).getText().toString(); 
		groupname = ((EditText) findViewById(R.id.groupnamein)).getText().toString(); 
		deadline = ((EditText) findViewById(R.id.deadlinein)).getText().toString();
		assignedby = ((EditText) findViewById(R.id.assignedbyin)).getText().toString();
		if ( ((CheckBox) findViewById(R.id.finishedCheckBox)).isChecked() == true)
			finished = 1;
		else 
			finished = 0;
		
		String[] test = deadline.split("/");
		if(test.length != 3 || test[0].length() != 4 || test[1].length() != 2 || test[2].length() != 2){
			
			SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy/MM/dd");
		    Date now = new Date();
		    deadline = sdfDate.format(now).replaceAll("-","/");
			
		}
		
		String toBeInserted = name+","+description+","+deadline+","+assignedby+","+groupname+","+groupid+
				","+finished+",0,"+guid+","+deleted;
	
		//connect to database to edit the task
		DatabaseConnection db = new DatabaseConnection(this);
        db.connectDatabase("TaskBoard","Tasks");
        db.update( id,toBeInserted);
		db.close();
		
		//send the new task to server
		
		try{
			Synchronized th = new Synchronized();
			th.db = new DatabaseConnection(this);
			th.run();

        }
        catch(Exception e){
        	new AlertDialog.Builder(this)
        	.setTitle("Server Connection Error")
        	.setMessage(e.getMessage().toString())
	        .setNeutralButton(android.R.string.cancel,
	            new DialogInterface.OnClickListener() {
		           public void onClick(DialogInterface dialog, int whichButton){
		        	   
		        	   finish();
		           
		           }
	        })
	        .show();
        	
        }
        finish();
        
	}

	public void deleteButtonOnClick(View view){
		
		//connect to database to delete current task
		DatabaseConnection db = new DatabaseConnection(this);
        db.connectDatabase("TaskBoard","Tasks");
        String toBeInserted = name+","+description+","+deadline+","+assignedby+","+groupname+","+groupid+
				","+finished+",0,"+guid+",1";
        db.update( id,toBeInserted);
		db.close();
		
		//send the new task to server
		
		try{
			Synchronized th = new Synchronized();
			th.db = new DatabaseConnection(this);
			th.run();

        }
        catch(Exception e){
        	new AlertDialog.Builder(this)
        	.setTitle("Server Connection Error")
        	.setMessage(e.getMessage().toString())
	        .setNeutralButton(android.R.string.cancel,
	            new DialogInterface.OnClickListener() {
		           public void onClick(DialogInterface dialog, int whichButton){
		        	   
		        	   finish();
		           
		           }
	        })
	        .show();
        	
        }
		
		Intent i = new Intent(TaskPage.this, ManageTasks.class);
		startActivity(i);
      	
        
		
	}
	
}


